package com.gimi.cloud.bbp.client.controller.product;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.client.service.product.ProductService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.product.ProductDto;
import com.gimi.cloud.bbp.dto.product.ProductSkuDto;

@RestController
@RequestMapping(value = "product")
public class
ProductCtroller {
	@Autowired
	private ProductService productService;
	private final static Logger logger = LoggerFactory.getLogger(ProductCtroller.class);
	
	@RequestMapping(value = "/getProductDetail")
	public void getProductDetail(@RequestBody ProductDto productDto,HttpServletResponse response) throws Exception {
		logger.info("请求参数==>"+productDto.getProdId());
		Response resp = new Response();
		ProductDto dto = productService.getProductDetail(productDto);
        resp.setRes_data(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	@RequestMapping(value = "/getProductProp")
	public void getProductProp(@RequestBody ProductDto productDto,HttpServletResponse response) throws Exception {
		logger.info("请求参数==>"+productDto);
		Response resp = new Response();
		List<Map<String,Object>> prop = productService.getProductProp(productDto);
		resp.setRes_data(prop);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	@RequestMapping(value = "/getModelValueList")
	public void getModelValueList(@RequestBody ProductDto productDto,HttpServletResponse response) throws Exception {
		logger.info("请求参数==>"+productDto);
		Response resp = new Response();
		List<ProductSkuDto> list = productService.getModelValueList(productDto);
		resp.setRes_data(list);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/getProductsOfGrade")
	public void getProductsOfGrade(@RequestBody ProductDto productDto,HttpServletResponse response) throws Exception {
		Response resp = new Response();
		List<String> thirdGradeCategories;
		if(StringUtil.isNotBlank(productDto.getSecondCatId())){
			thirdGradeCategories = productService.getChildGradeCategories(productDto.getSecondCatId(), 3);
		}else{
			thirdGradeCategories = productService.getThirdGradeCategoriesByFirstGradeCategoryId(productDto.getFirstCatId());
		}
		Map<String , List<ProductDto>> result = new HashMap<>();
		result.put("products", productService.getProductsOfThirdGrade(thirdGradeCategories));
        resp.setRes_data(result);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/searchProducts")
	public void searchProducts(@RequestBody ProductDto productDto,HttpServletResponse response) throws Exception {
		Response resp = new Response();
		Map<String , List<ProductDto>> result = new HashMap<>();
		result.put("products", productService.searchProducts(productDto));
        resp.setRes_data(result);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
}
